package client.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

/**
 * Provides a toolkit of utility methods for creating GUIs.
 *
 * @author Brent Ramerth
 * @version 1.0
 * @since Jul 7, 2005 12:04:22 AM 
 */
public class GUITools {

    /**
     * Returns a JPanel to hold a JLabel and the provided component, with an optional margin between
     * the label and component.
     * @param c a Component to be labeled
     * @param text the text of the label
     * @param position the position of the label relative to the component (use BorderLayout constants)
     * @param margin if the position is BorderLayout.NORTH, the margin between the label and the component
     * @return a JPanel with the labeled component
     */
	public static JPanel labelComponent(Component c, String text, String position, int margin)
	{
		JPanel p = new JPanel(new BorderLayout());
		p.setOpaque(false);
		JLabel l = new JLabel(text);
		l.setVerticalAlignment(JLabel.CENTER);
		//l = (JLabel)Helvetica.convert(l, 12, position==BorderLayout.NORTH);
		if (margin > 0 && position==BorderLayout.NORTH) 
			p.add(addMargins(l, new Insets(0,0,margin,0)), position);
		else
			p.add(l,position);

		p.add(c, BorderLayout.CENTER);
		return p;
	}

	/**
	 * Returns a JPanel to hold a JLabel and the provided component.
	 * @param c a Component to be labeled
	 * @param text the text of the label
	 * @param position the position of the label relative to the component (use BorderLayout constants)
	 * @return a JPanel with the labeled component
	 */
	public static JPanel labelComponent(Component c, String text, String position) {
    	return labelComponent(c,text,position,0);
	}

	/**
	 * Returns a JPanel that holds the Component surrounded by an EmptyBorder whose size is determined
	 * by the given Insets.
	 * @param c a Component
	 * @param i an Insets
	 * @return the JPanel
	 */
	public static JPanel addMargins(Component c, Insets i) {
		JPanel p = (c instanceof JPanel? (JPanel)c: new JPanel(new BorderLayout()));
		p.setOpaque(false);
		p.setBackground(new Color(215,215,233));
		p.setBorder(new EmptyBorder(i));
		if (!(c instanceof JPanel)) p.add(c);
		return p;
	}

	/**
	 * Returns the center point of a Component.
	 * @param c a Component
	 * @return the center
	 */
	public static Point getCenterPoint(Component c) {
		Component parent = c.getParent();
		return new Point(parent.getX() + (parent.getWidth() - c.getWidth())/2, 
				parent.getY() + (parent.getHeight() - c.getHeight())/ 2);
	}
}
